<template>
  <div class="login-body">
    <Particles id="tsparticles" class="login__particles" :options="options"/>
    <div class="login-panel">
      <div class="login-title">用户登录</div>
      <el-form :model="formData" :rules="rules" ref="formDataRef">
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" v-model="formData.username" size="large" type="text">
            <template #prefix>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="formData.password" size="large" type="password"
                    @keyup.enter.native="login()">
            <template #prefix>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="">
            <div class="check-code-panel">
                <el-input placeholder="请输入验证码" v-model="formData.checkCode" class="input-panel" />
                <img src="checkCodeUrl" class="check-code">
            </div>
        </el-form-item> -->
        <!-- <el-form-item label="">
            <el-checkbox label="记住密码" name="type" />
        </el-form-item> -->
        <el-form-item label="">
          <el-button type="primary" style="width: 100%;" @click="login()" size="large">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, } from 'vue'
import { ElMessage } from 'element-plus';
// import request from '@/utils/request';		//这里使用自行封装的axios，下文已给出，照搬后修改运行端口即可
import { useRouter } from 'vue-router';
import axios from "axios";
import {postUserInfo} from "../../api/user";
// const checkCodeUrl = "api/checkCode?" + new Date().getTime();
//表单
const formDataRef = ref();
let formData = reactive({
  username: "",
  password: ""
});
const rules = {
  username: [{
    required: true,
    message: "请输入用户名"
  }],
  password: [{
    required: true,
    message: "请输入密码"
  }],
  // checkCode: [{
  //     required: true,
  //     message: "请输入验证码"
  // }],
}

const router = useRouter();

const login = () => {
  // router.push("/register")
  var form_obj = JSON.parse(JSON.stringify(formData));
  console.log(form_obj);
  console.log(form_obj.username);
  console.log(form_obj.password);
  postUserInfo({
    "username":form_obj.username,
    "password":form_obj.password
  }).then((res)=>{
    console.log(res)
    if(res.code === 200){
      router.push("/register")
    }else{
      ElMessage.error(res.msg)
    }
  }).catch((err)=>{
    console.log(err)
  })
  // axios.post("/api/vue/vite/login",{
  //   "username":form_obj.username,
  //   "password":form_obj.username
  // }).then((res)=>{
  //   console.log(res.data)
  //   if(res.data.code === 200){
  //
  //   }else{
  //     ElMessage.error(res.data.msg)
  //   }
  // }).catch((err)=>{
  //   console.log(err)
  // })
}
  // var form_obj = JSON.parse(JSON.stringify(formData));
  // console.log(form_obj);
  // console.log(form_obj.username);
  // console.log(form_obj.password);

  // 后端代码自行准备
  // request.post("/user/login", form_obj).then(res => {
  //   if (res) {
  //     ElMessage({
  //       message: '登录成功',
  //       type: 'success',
  //     })
  //
  //     let tokenObj = { token: " isLogin", startTime: new Date().getTime() };
  //     window.localStorage.setItem("isLogin", JSON.stringify(tokenObj));
  //     localStorage.setItem("username", JSON.parse(JSON.stringify(formData.username)));
  //
  //     router.push("/");
  //
  //   } else {
  //     ElMessage.error('账号或密码错误！！！登录失败！！！')
  //   }
  // });
// };

const options = {
  fpsLimit: 60,
  interactivity: {
    detectsOn: "canvas",
    events: {
      onClick: {
        // 开启鼠标点击的效果
        enable: true,
        mode: "push",
      },
      onHover: {
        // 开启鼠标悬浮的效果(线条跟着鼠标移动)
        enable: true,
        mode: "grab",
      },
      resize: true,
    },
    modes: {
      // 配置动画效果
      bubble: {
        distance: 400,
        duration: 2,
        opacity: 0.8,
        size: 40,
      },
      push: {
        quantity: 4,
      },
      grab: {
        distance: 200,
        duration: 0.4,
      },
      attract: {
        // 鼠标悬浮时，集中于一点，鼠标移开时释放产生涟漪效果
        distance: 200,
        duration: 0.4,
        factor: 5,
      },
    },
  },
  particles: {
    color: {
      value: "#3bdb1f", // 粒子点的颜色
    },
    links: {
      color: "#82c67d", // 线条颜色
      distance: 150, //线条距离
      enable: true,
      opacity: 0.4, // 不透明度
      width: 1.2, // 线条宽度
    },
    collisions: {
      enable: true,
    },
    move: {
      attract: {enable: false, rotateX: 600, rotateY: 1200},
      bounce: false,
      direction: "none",
      enable: true,
      out_mode: "out",
      random: false,
      speed: 0.5, // 移动速度
      straight: false,
    },
    number: {
      density: {
        enable: true,
        value_area: 800,
      },
      value: 80, //粒子数
    },
    opacity: {
      //粒子透明度
      value: 0.7,
    },
    shape: {
      //粒子样式
      type: "star",
    },
    size: {
      //粒子大小
      random: true,
      value: 3,
    },
  },
  detectRetina: true,
};

</script>

<style lang="scss" scoped >
.login-body {
  background: url("src/assets/images/wallhaven-rrqdr1.png") no-repeat center center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: absolute;
  left: 0;
  top: 0;

  .login-panel {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;

    padding: 25px;
    width: 26%;
    min-width: 460px;
    height: 30%;
    min-height: 300px;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 5%;
    box-shadow: 2px 2px 10px #ddd;

    .login-title {
      font-size: 22px;
      text-align: center;
      margin-bottom: 22px;
    }
  }
}
</style>
